import requests
import os
from datetime import datetime

# 手动执行
url = 'http://192.168.19.159:8000/pdf'

path = os.path.join(os.getcwd(), "files_dir")  # pyproject2/files_dir

# 读取文件内容  设置返回名字
def reqeust_file(filename):
    with open(os.path.join(path, filename), 'rb') as f:
        files = {"file": (filename, f.read())}

    response = requests.post(url, files=files)
    print(response.text)

# 遍历文件夹  文件夹位置固定
def send_files(path):
    dirs_ = os.listdir(path)
    print(dirs_)
    # 发送文件 判断文件名称
    for filename in dirs_:
        if 'log' in filename:
            continue
        reqeust_file(filename)
        # 文件发送后 删除本地文件夹内的代码 并写入本地日志
        with open(os.path.join(path,"request_log.log"),"a") as f:
            f.write(filename + " 已发送" + " "*5 + datetime.now().strftime("%Y%m%d-%H:%M") + '\n')


send_files(path)